var app=new Vue({
    el: '#app',
    data:{
        user:{"userType":1},
        isDisabled: false,
        dicts:{
            depts: [],
            roles: []
        },
        userRules: {
            userName: [
                { required: true,validator: validateUserName, trigger: 'blur' }
            ],
            realName: [
                { required: true,min: 2, max: 10, message: '用户姓名长度在 2 到 10 个字符', trigger: 'blur' }
            ],
            email: [
                { type: 'email', required: false, message: '请填写正确的邮箱', trigger: 'change' }
            ],
            phone: [
                {required: true, validator: validatePhone, trigger: 'blur' }
            ],
            roleId: [
                { required: true, message: '请至少选择一个角色', trigger: 'change' }
            ],
            geneder: [
                { required: false, message: '请至少选择一个性别', trigger: 'change' }
            ],
            remark: [
                { max:100, message: '备注最大长度为100', trigger: 'blur' }
            ]
        }
    },
    methods:{
        init: function(){
            var _this=this;
            setTimeout(function(){
                _this.initDict();
            },100);
        },
        closeDialog: function(){
            jsmile.dialog.close();
        },
        insertUser: function(){
            var _this=this;
            _this.isDisabled=true;
            this.$refs['user'].validate(function(valid){
                if (valid) {
                    var _data=jsmile.copy({},_this.user);
                    _data.deptId=_this.user.deptId?_this.user.deptId[_this.user.deptId.length-1]:null;
                    jsmile.http.sendJson({
                        url:jsmile.url+"/sys/user/save",
                        data:_data
                    }).done(function(res){
                        jsmile.msg.success("添加用户信息成功!");
                        jsmile.dialog.close();
                        top.$('#home')[0].contentWindow.app.query();
                    }).fail(function(res){
                        jsmile.msg.error(res.resMsg?res.resMsg:"添加用户失败!");
                    });
                } else {
                    return false;
                }
            });
        },
        /**
         * 初始化数据字典
         */
        initDict: function(){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/dept/findAll",
                data:{}
            }).done(function(res){
                var data=jsmile.tree.toTree(res.resData.list,{"textField":"deptName"});
                _this.dicts.depts=data;
            });
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/role/findAll"
            }).done(function(res){
                _this.dicts.roles=res.resData.list;
            });
        }
    }
});
$(function(){
    init();
    function init(){
        app.init();
    }
});